package momo.app.multicast.presence;

import java.util.HashMap;

public class PresenceMessage {
    private static String SPLITTER = "<presenceMessageSplitter>";
    private String user_name;
    private int version;
    private HashMap<String, Integer> vector_clock;
    
    public PresenceMessage(String user_name, int version,
                           HashMap<String, Integer> vector_clock) {
        this.user_name = user_name;
        this.version = version;
        this.vector_clock = vector_clock;
    }
    
    public PresenceMessage(String object_string) {
        String[] temp = object_string.trim().split(SPLITTER);
        this.user_name = temp[0];
        this.version = Integer.parseInt(temp[1]);
        this.vector_clock = new HashMap<String, Integer>();
        String[] vectorEntry = temp[2].substring(1, temp[2].length() - 1).split(", ");
        for (int i = 0; i < vectorEntry.length; i++) {
            String[] pair = vectorEntry[i].split("=");
            this.vector_clock.put(pair[0], Integer.parseInt(pair[1]));
        }
    }
    
    public String getUserName() {
        return user_name;
    }
    
    public int getVersion() {
        return version;
    }
    
    public HashMap<String, Integer> getVectorClock() {
        return vector_clock;
    }
    
    public String toString() {
        return user_name + SPLITTER + version + SPLITTER + vector_clock.toString();
    }
}
